package org.example.dbTest.Manticore

import dbTest.Manticore.ManticoreClient
import io.micrometer.core.instrument.Gauge
import kotlinx.coroutines.*
import test.t1.MicrometerStandalone
import java.util.concurrent.atomic.AtomicLong
import java.util.concurrent.atomic.LongAdder

val scope = CoroutineScope(Dispatchers.Default)

suspend fun main() {

//    speed { i->
//        val doc = mapOf(
//            "title" to "Smartphone $i",
//            "content" to "Latest model smartphone with advanced features $i",
//            "category" to "Electronics $i",
//            "price" to 599.99,
//            "published" to true
//        )
//        manticore.insertDocument("products", doc, i.toLong())
//    }

//    speed { i->
//        manticore.executeSql("SELECT * FROM products where MATCH('Smartphone ') limit 10 ")
////        manticore.searchDocuments("products", "$i")
//    }

//    val searchResult = manticore.searchDocuments("products", "shoes")
//    println("Search results: $searchResult")

//    println(manticore.executeSql("TRUNCATE TABLE products"))
//    println(manticore.executeSql("SELECT count(*) FROM products"))
//    println("SQL query result: ${manticore.executeSql("SELECT * FROM products WHERE price > 0")}")

    // 搜索所有文档
//    val allResults = manticore.searchDocuments("products")
//    println("All documents: $allResults")


//    manticore.deleteDocument("products", 1)
}

fun insert(manticore: ManticoreClient) {
    for (i in 10..500) {
        val doc = mapOf(
            "title" to "Smartphone $i",
            "content" to "Latest model smartphone with advanced features $i",
            "category" to "Electronics $i",
            "price" to 599.99,
            "published" to true
        )
        manticore.insertDocument("products", doc, i.toLong())
    }
}


fun createTable(manticore: ManticoreClient) {
    // 创建表
    val fields = mapOf(
        "title" to "text",
        "content" to "string attribute indexed",
        "category" to "string attribute",
        "price" to "float",
        "published" to "bool"
    )

    println("Creating table 'products'...")
    manticore.createTable("products", fields)
}

fun del(manticore: ManticoreClient) {
    // 更新文档
    println("\nUpdating document...")
    val updatedDoc = mapOf(
        "title" to "Running Shoes Pro",
        "content" to "Professional running shoes for athletes",
        "category" to "Sports",
        "price" to 159.99,
        "published" to true
    )
    manticore.updateDocument("products", 3, updatedDoc)

    // 替换文档
    println("\nReplacing document...")
    val replacedDoc = mapOf(
        "title" to "Gaming Laptop",
        "content" to "High performance gaming laptop with RGB keyboard",
        "category" to "Electronics",
        "price" to 1299.99,
        "published" to true
    )
    manticore.replaceDocument("products", 2, replacedDoc)
}


suspend fun speed(action: (i: Int) -> Unit) {
    val startTime = System.currentTimeMillis()
    val count = 1000000
    val jobs = mutableListOf<Job>()

    repeat(count) { i->
        val job = scope.launch {
            action(i)
        }
        jobs.add(job)
    }

    // 运行指定时间
    jobs.joinAll();

    val endTime = System.currentTimeMillis()
    val durationSeconds = (endTime - startTime) / 1000.0

    println("耗时: $durationSeconds , qps: ${count / durationSeconds}")
}
